package ShareLife.gar;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

public class FriendItem {

	public String phonephonenumber;
	public String name;
	public Bitmap photoImage;
	PlayerInfo testtemp;
	public int photowidth;
	public int photoheight;
	
	public FriendItem(PlayerInfo test) {
		testtemp = test;
	}

	public void writeinfor(DataOutputStream dos) throws IOException {

	}
	public void readinfor(DataInputStream dis) throws IOException {
		phonephonenumber = dis.readUTF();
		name = dis.readUTF();
		photowidth = dis.readInt();
		photoheight = dis.readInt();
		int imagesize = ((photowidth * photoheight) << 2);
		byte []photoData = new byte[imagesize];
		receiveByteArray(dis, photoData);
		int [] temparray = ToolOld.byte2int(photoData);
		photoImage = contructPhoto(temparray);
		try {
			Thread.sleep(150);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void receiveByteArray(DataInputStream dis, byte Data[]) {

		int currentIndex = 0;
		int storebytenumber = 0;
		while (currentIndex != Data.length) {
			storebytenumber = Data.length - currentIndex >= 512 ? 512
					: Data.length - currentIndex;
			byte temparray[] = new byte[storebytenumber];
			try {
				dis.read(temparray, 0, storebytenumber);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.arraycopy(temparray, 0, Data, currentIndex, storebytenumber);
			currentIndex += storebytenumber;

		}

	}
	
	public Bitmap contructPhoto(int photoData[])
	{
//		return BitmapFactory.decodeStream(new ByteArrayInputStream(photoData));
		
		return Bitmap.createBitmap(photoData, photowidth, photowidth,Config.ARGB_8888);
		
	}

	
}


